package com.KKd.controller;

import com.KKd.pojo.Chain;
import com.KKd.service.ChainServiceImpl;
import com.KKd.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.WebParam;
import java.util.Date;
import java.util.List;

/**
 * @author 14587
 * @date 2020/11/14 16:37
 */
@RequestMapping("/chain")
@Controller
public class ChainController {
    @Autowired
    private ChainServiceImpl chainService;
    //前往友链页面
    @RequestMapping("/toChainPage")
    public String toChainPage(Model model){
        List<Chain> chains = chainService.queryChains();
        model.addAttribute("chains",chains);
        return "chainManager";
    }
    //友链置顶
    @RequestMapping("/setFirstChain/{chainId}")
    public String setFirstChain(@PathVariable int chainId){
        chainService.setChainFirstById(chainId);
        return "redirect:/page/showPage";
    }
    //添加友链
    @RequestMapping("/addChain")
    public String addChain(String link,String chainName){
        if (link!=null&&chainName!=null){
            String date = JsonUtils.getJson(new Date());
            chainService.addChain(new Chain(null,link,false,chainName,date));
        }
        return "forward:/chain/toChainPage";
    }
    //删除友链
    @RequestMapping("/deleteChain/{chainId}")
    public String deleteChain(@PathVariable Integer chainId){
        chainService.deleteChainById(chainId);
        return "redirect:/chain/toChainPage";
    }
}
